package com.caipos.actor.ruleChain;

import com.caipos.actor.ActorSystemContext;
import com.caipos.actor.common.actors.KbActor;
import com.caipos.actor.common.actors.KbActorId;
import com.caipos.actor.common.actors.KbEntityActorId;
import com.caipos.actor.common.actors.KbActorCtx;
import com.caipos.actor.common.data.id.RuleChainId;
import com.caipos.actor.common.data.id.RuleNodeId;
import com.caipos.actor.common.data.id.TenantId;
import com.caipos.actor.common.msg.KbActorMsg;
import com.caipos.actor.common.msg.KbMsg;
import com.caipos.actor.service.ContextBasedCreator;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RuleNodeActor extends RuleEngineComponentActor<RuleNodeId, RuleNodeActorMessageProcessor> {

    private final String ruleChainName;
    private final RuleChainId ruleChainId;
    private final RuleNodeId ruleNodeId;

    private RuleNodeActor(ActorSystemContext systemContext, TenantId tenantId,
                          RuleChainId ruleChainId, String ruleChainName, RuleNodeId ruleNodeId) {
        super(systemContext, tenantId, ruleNodeId);
        this.ruleChainName = ruleChainName;
        this.ruleChainId = ruleChainId;
        this.ruleNodeId = ruleNodeId;
    }

    @Override
    protected RuleNodeActorMessageProcessor createProcessor(KbActorCtx ctx) {
        return new RuleNodeActorMessageProcessor(tenantId, this.ruleChainName, ruleNodeId, systemContext, ctx);
    }

    @Override
    protected boolean doProcess(KbActorMsg msg) {
        switch (msg.getMsgType()) {
            case COMPONENT_LIFE_CYCLE_MSG:
//            case RULE_NODE_UPDATED_MSG:
//                onComponentLifecycleMsg((ComponentLifecycleMsg) msg);
//                break;
            case RULE_CHAIN_TO_RULE_MSG:
                onRuleChainToRuleNodeMsg((RuleChainToRuleNodeMsg) msg);
                break;
            case RULE_TO_SELF_MSG:
                onRuleNodeToSelfMsg((RuleNodeToSelfMsg) msg);
                break;
//            case STATS_PERSIST_TICK_MSG:
//                onStatsPersistTick(id);
//                break;
//            case PARTITION_CHANGE_MSG:
//                onClusterEventMsg((PartitionChangeMsg) msg);
//                break;
            default:
                return false;
        }
        return true;
    }

    private void onRuleNodeToSelfMsg(RuleNodeToSelfMsg msg) {
        if (log.isDebugEnabled()) {
            log.debug("[{}][{}][{}] Going to process rule msg: {}", ruleChainId, id, processor.getComponentName(), msg.getMsg());
        }
        try {
//            processor.onRuleToSelfMsg(msg);
//            increaseMessagesProcessedCount();
        } catch (Exception e) {
            logAndPersist("onRuleMsg", e);
        }
    }

    private void onRuleChainToRuleNodeMsg(RuleChainToRuleNodeMsg envelope) {
        KbMsg msg = envelope.getMsg();
//        if (!msg.isValid()) {
//            if (log.isTraceEnabled()) {
//                log.trace("Skip processing of message: {} because it is no longer valid!", msg);
//            }
//            return;
//        }
        if (log.isDebugEnabled()) {
            log.debug("[{}][{}][{}] Going to process rule engine msg: {}", ruleChainId, id, processor.getComponentName(), msg);
        }
        try {
//            processor.onRuleChainToRuleNodeMsg(envelope);
//            increaseMessagesProcessedCount();
        } catch (Exception e) {
            logAndPersist("onRuleMsg", e);
        }
    }

    public static class ActorCreator extends ContextBasedCreator {

        private final TenantId tenantId;
        private final RuleChainId ruleChainId;
        private final String ruleChainName;
        private final RuleNodeId ruleNodeId;

        public ActorCreator(ActorSystemContext context, TenantId tenantId, RuleChainId ruleChainId, String ruleChainName, RuleNodeId ruleNodeId) {
            super(context);
            this.tenantId = tenantId;
            this.ruleChainId = ruleChainId;
            this.ruleChainName = ruleChainName;
            this.ruleNodeId = ruleNodeId;
        }

        @Override
        public KbActorId createActorId() {
            return new KbEntityActorId(ruleNodeId);
        }

        @Override
        public KbActor createActor() {
            return new RuleNodeActor(context, tenantId, ruleChainId, ruleChainName, ruleNodeId);
        }
    }

    @Override
    protected RuleChainId getRuleChainId() {
        return ruleChainId;
    }

    @Override
    protected String getRuleChainName() {
        return ruleChainName;
    }

    @Override
    protected long getErrorPersistFrequency() {
        return systemContext.getRuleNodeErrorPersistFrequency();
    }

}
